package org.go.smart.temperature;

import org.go.smart.HouseController;
import org.go.smart.RoomController;

public class TemperatureChecker implements Runnable {

	@Override
	public void run() {
		HouseController houseController = HouseController.getInstance();
		while(true) {
			if (houseController.isHeaterActive()) {
				for (String roomControllerName : houseController.getRoomControllers().keySet()) {
					RoomController roomController = houseController.getRoomController(roomControllerName);
					if (roomController.shouldStopHeater()) {
						houseController.stopHeater();
						break;
					}
				}
			} else {
				for (String roomControllerName : houseController.getRoomControllers().keySet()) {
					RoomController roomController = houseController.getRoomController(roomControllerName);
					if (roomController.shouldStartHeater()) {
						houseController.startHeater();
						break;
					}
				}
			}
			
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}
